//
//  DGGNetworkManager.m
//  DGGNetworkingSDK
//
//  Created by Dwt on 2019/12/25.
//

#import "DGGNetworkManager.h"
#import "DGGNetworkHelper.h"
#import <AFNetworking/AFNetworking.h>

typedef NS_ENUM(NSUInteger, DGGNetworkParamType) {
    DGGNetworkParams = 0,    //参数以params类型
    DGGNetworkParamBody      //参数以body类型
};

@interface DGGNetworkManager ()

/// 工具
@property (nonatomic, strong) DGGNetworkHelper *helper;
/// manager
@property (nonatomic, strong) AFHTTPSessionManager *manager;
/// 当前请求任务
@property (nonatomic, strong) NSURLSessionDataTask *currentTask;
/// 请求
@property (nonatomic, strong) NSMutableURLRequest *request;
/// 当前请求类型
@property (nonatomic, assign) DGGNetworkParamType currentParamType;


@end

static NSString *const kContentTypeKey = @"Content-Type";
static NSString *const kContentType = @"application/x-www-form-urlencoded";
static NSString *const kFileAcceptType = @"application/json";
static NSString *const kMultiPartMimeType = @"multipart/form-data";

@implementation DGGNetworkManager

#pragma mark - 单例
+ (instancetype)shareInstance
{
    static DGGNetworkManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DGGNetworkManager alloc]init];
    });
    return manager;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self commonConfig];
    }
    return self;
}

#pragma mark - GET
- (void)get:(NSString *)url
      param:(NSDictionary *)param
     result:(ResultCallBack)result
{
    [self get:url header:nil param:param result:result];
}

- (void)get:(NSString *)url header:(NSDictionary *)header param:(NSDictionary *)param result:(ResultCallBack)result{
    self.currentParamType = DGGNetworkParams;
    
    //网络异常
    if (![self handleNoNetwork:result]) return;
    
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:param];
    
    //get请求验签处理
    NSString *finalUrl = url;
    if (self.signGetUrlCallBack) {
        finalUrl = self.signGetUrlCallBack(url, parameters);
    }

//    [DLAccountInfo ]
    
    [header enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [self.manager.requestSerializer setValue:obj forHTTPHeaderField:key];
    }];
    
//    //设置验签请求头
//    [self setSignHeader:DGGNetworkRequestGET
//                    url:finalUrl
//                 header:header
//                  param:parameters
//              paramType:DGGNetworkParams];
    
    @weakify(self);
    self.currentTask = [self.manager GET:finalUrl
                              parameters:self.signGetUrlCallBack ? @{} : parameters
                                progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:responseObject];
        
        [self handleSuccess:responseObject result:result];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:error];
        
        [self handleError:error result:result];
    }];
}

#pragma mark - POST
- (void)post:(NSString *)url
       param:(NSDictionary *)param
      result:(ResultCallBack)result{
    [self post:url header:nil param:param result:result];
}

- (void)post:(NSString *)url
      header:(NSDictionary *)header
       param:(NSDictionary *)param
      result:(ResultCallBack)result
{
    self.currentParamType = DGGNetworkParams;
    
    //网络异常
    if (![self handleNoNetwork:result]) return;
    
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:param];
    
    [header enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//        AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
//        [requestSerializer setValue:obj forHTTPHeaderField:key];
//        self.manager.requestSerializer = requestSerializer;
        [self.manager.requestSerializer setValue:obj forHTTPHeaderField:key];
    }];
    
    
//    //设置验签请求头
//    [self setSignHeader:DGGNetworkRequestPOST
//                    url:url
//                 header:header
//                  param:parameters
//              paramType:DGGNetworkParams];
    
    @weakify(self);
    self.currentTask = [self.manager POST:url
                               parameters:parameters
                                 progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:responseObject];
        
        [self handleSuccess:responseObject result:result];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:error];
        
        [self handleError:error result:result];
    }];
}

#pragma mark - PUT
- (void)put:(NSString *)url
      param:(NSDictionary *)param
     result:(ResultCallBack)result{
    [self put:url header:nil param:param result:result];
}

- (void)put:(NSString *)url
     header:(NSDictionary *)header
      param:(NSDictionary *)param
     result:(ResultCallBack)result
{
    self.currentParamType = DGGNetworkParams;
    
    //网络异常
    if (![self handleNoNetwork:result]) return;
    
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:param];
    
    //设置验签请求头
    [self setSignHeader:DGGNetworkRequestPOST
                    url:url
                 header:header
                  param:parameters
              paramType:DGGNetworkParams];
    
    @weakify(self);
    self.currentTask = [self.manager PUT:url
                              parameters:parameters
                                 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:responseObject];
        
        [self handleSuccess:responseObject result:result];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:error];
        
        [self handleError:error result:result];
    }];
}


#pragma mark - Multi-part
- (void)multiPost:(NSString *)url
            param:(NSDictionary *)param
         filePath:(NSURL *)filePath
         fileName:(NSString *)fileName
  requestFileName:(NSString *)requestFileName
           result:(ResultCallBack)result{
    [self multiPost:url header:nil param:param filePath:filePath fileName:fileName requestFileName:requestFileName result:result];
}

- (void)multiPost:(NSString *)url
           header:(NSDictionary *)header
            param:(NSDictionary *)param
         filePath:(NSURL *)filePath
         fileName:(NSString *)fileName
  requestFileName:(NSString *)requestFileName
           result:(ResultCallBack)result
{
    self.currentParamType = DGGNetworkParams;
    
    //网络异常
    if (![self handleNoNetwork:result]) return;
    
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:param];
    
    //设置验签请求头
    [self setSignHeader:DGGNetworkRequestPOST
                    url:url
                 header:header
                  param:parameters
              paramType:DGGNetworkParams];
    
    [self.manager.requestSerializer setValue:kFileAcceptType forHTTPHeaderField:@"Accept"];
    
    @weakify(self);
    self.currentTask = [self.manager POST:url
                               parameters:parameters
                constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [formData appendPartWithFileURL:filePath
                                   name:requestFileName
                               fileName:fileName
                               mimeType:kMultiPartMimeType
                                  error:nil];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:responseObject];
        
        [self handleSuccess:responseObject result:result];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:error];
        
        [self handleError:error result:result];
    }];
}

#pragma mark - Multi-part for Data
- (void)multiPost:(NSString *)url
            param:(NSDictionary *)param
         fileData:(NSData *)fileData
         fileName:(NSString *)fileName
  requestFileName:(NSString *)requestFileName
           result:(ResultCallBack)result{
    [self multiPost:url header:nil param:param fileData:fileData fileName:fileName requestFileName:requestFileName result:result];
}

- (void)multiPost:(NSString *)url
           header:(NSDictionary *)header
            param:(NSDictionary *)param
         fileData:(NSData *)fileData
         fileName:(NSString *)fileName
  requestFileName:(NSString *)requestFileName
           result:(ResultCallBack)result{
    
    self.currentParamType = DGGNetworkParams;
    
    //网络异常
    if (![self handleNoNetwork:result]) return;
    
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:param];
    
    //设置验签请求头
    [self setSignHeader:DGGNetworkRequestPOST
                    url:url
                 header:header
                  param:parameters
              paramType:DGGNetworkParams];
    
    [self.manager.requestSerializer setValue:kFileAcceptType forHTTPHeaderField:@"Accept"];
    
    @weakify(self);
    self.currentTask = [self.manager POST:url
                               parameters:parameters
                constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [formData appendPartWithFileData:fileData name:requestFileName fileName:fileName mimeType:kMultiPartMimeType];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:responseObject];
        
        [self handleSuccess:responseObject result:result];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:error];
        
        [self handleError:error result:result];
    }];
}

#pragma mark - POST携请求体
- (void)post:(NSString *)url
        body:(NSDictionary *)body
      result:(ResultCallBack)result{
    [self post:url header:nil body:body result:result];
}

- (void)post:(NSString *)url
      header:(NSDictionary *)header
        body:(NSDictionary *)body
      result:(ResultCallBack)result
{
    self.currentParamType = DGGNetworkParamBody;
    
    //网络异常
    if (![self handleNoNetwork:result]) return;
    
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:body];
    
    self.request.URL = [NSURL URLWithString:url ?: @""];
    self.request.HTTPMethod = @"POST";
    
    //设置body
    if (parameters) {
        NSData *tData = [NSJSONSerialization dataWithJSONObject:parameters
                                                        options:NSJSONWritingPrettyPrinted
                                                          error:nil];
        [self.request setHTTPBody:tData];
    }
    
    //设置验签请求头
    [self setSignHeader:DGGNetworkRequestPOST
                    url:url
                 header:header
                  param:parameters
              paramType:DGGNetworkParamBody];
    

    
    [[self.manager dataTaskWithRequest:self.request
                     completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error) {
            if (result) {
                NSDictionary *tJson = [NSJSONSerialization JSONObjectWithData:[NSJSONSerialization dataWithJSONObject:responseObject
                                                                                                              options:NSJSONWritingPrettyPrinted
                                                                                                                error:nil]
                                                                      options:NSJSONReadingMutableLeaves
                                                                        error:nil];
                [self clearConfig];
                
                result(tJson);
            }
            
        } else {
            
            [self handleError:error result:result];
        }
        
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParamBody
      responseObject:error ? error : responseObject];
    }] resume] ;
    
}

#pragma mark - 下载文件
- (void)downLoadFileWithURL:(NSString *)url
                  cachePath:(NSString *)cachePath
                     params:(NSDictionary *)params
                    headers:(NSDictionary *)headers
                   progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock
                     result:(ResultCallBack)result{
    self.currentParamType = DGGNetworkParams;
    //网络异常
    if (![self handleNoNetwork:result]) return;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url ?: @""] cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:self.timeout>0?self.timeout:20];
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:params];
    //设置body
    if (params) {
        NSData *tData = [NSJSONSerialization dataWithJSONObject:params
                                                        options:NSJSONWritingPrettyPrinted
                                                          error:nil];
        [request setHTTPBody:tData];
    }
    //设置验签请求头
    if (self.verifyHeaderCallBack || headers){
        NSDictionary *tDict = self.verifyHeaderCallBack(DGGNetworkRequestDownload, url, parameters, headers);
        for (NSString *tKey in self.manager.requestSerializer.HTTPRequestHeaders.allKeys) {
            [self.manager.requestSerializer setValue: @"" forHTTPHeaderField:tKey];
        }
        for (NSString *tKey in tDict.allKeys) {
            [request setValue:tDict[tKey] ?: @"" forHTTPHeaderField:tKey];
        }
        //自定义请求头内容
        for (NSString *tKey in headers.allKeys) {
            [request setValue:headers[tKey] ?: @"" forHTTPHeaderField:tKey];
        }
    }
    NSURLSessionDownloadTask *task = [self.manager downloadTaskWithRequest:request progress:downloadProgressBlock destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        if(cachePath && ![@"" isEqualToString:cachePath]){
            return [NSURL fileURLWithPath:cachePath];
        }
        return targetPath;
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if(!error){
            if (result) {
                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                NSMutableDictionary *responseObject = @{}.mutableCopy;
                [responseObject setValue:@"0" forKey:@"code"];
                [responseObject setValue:@([httpResponse statusCode]) forKey:@"originCode"];
                [responseObject setValue:filePath.path forKey:@"filePath"];
                result(responseObject);
            }else{
                NSLog(@"未实现回调");
            }
        }else{
            [self handleError:error result:result];
        }
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
        [self logUrl:url param:parameters paramType:DGGNetworkParamBody responseObject:error ? error : @{@"code":@([httpResponse statusCode]),@"filePath":filePath?filePath:@""}];
    }];
    [task resume];
}


#pragma mark - 上传文件
- (NSURLSessionDataTask *)post:(NSString *)url
                         param:(NSDictionary *)param
                         block:(void(^)(id formData))block
                      progress:(void(^)(NSProgress *uploadProgress))progress
                        result:(ResultCallBack)result
{
    self.currentParamType = DGGNetworkParams;
    //网络异常
    if (![self handleNoNetwork:result]) return nil;
    
    //添加公共参数
    NSDictionary *parameters = [self configCommonParam:param];
    
    //设置验签请求头
    [self setSignHeader:DGGNetworkRequestPOST
                    url:url
                 header:nil
                  param:parameters
              paramType:DGGNetworkParams];
    
    @weakify(self);
    NSURLSessionDataTask *task = [self.manager POST:url parameters:param constructingBodyWithBlock:block progress:progress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:responseObject];
        
        [self handleSuccess:responseObject result:result];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        @strongify(self);
        [self logUrl:url
               param:parameters
           paramType:DGGNetworkParams
      responseObject:error];
        
        [self handleError:error result:result];
    }];
    return task;
}

- (void)DELETE:(NSString *)url param:(NSDictionary *)param result:(void(^)(id result))result {
    self.currentTask = [self.manager DELETE:url parameters:param success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        result(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        result(error);
    }];
}

#pragma mark - 设置请求头
- (void)setSignHeader:(DGGNetworkRequestType)requestType
                  url:(NSString *)url
               header:(NSDictionary *)header
                param:(NSDictionary *)param
            paramType:(DGGNetworkParamType)paramType
{
    //配置特殊请求头
    [self handleContent_type:paramType];
    
    if (self.verifyHeaderCallBack || header){
//        NSDictionary *tDict = self.verifyHeaderCallBack(requestType, url, param, header);
        if (paramType == DGGNetworkParams) {
            for (NSString *tKey in self.manager.requestSerializer.HTTPRequestHeaders.allKeys) {
                [self.manager.requestSerializer setValue: @"" forHTTPHeaderField:tKey];
            }
//            for (NSString *tKey in tDict.allKeys) {
//                [self.manager.requestSerializer setValue:tDict[tKey] ?: @"" forHTTPHeaderField:tKey];
//            }
            //自定义请求头内容
            for (NSString *tKey in header.allKeys) {
                [self.manager.requestSerializer setValue:header[tKey] ?: @"" forHTTPHeaderField:tKey];
            }
            
            for (NSString *tKey in self.httpHeaders.allKeys) {
                [self.manager.requestSerializer setValue:self.httpHeaders[tKey] ?: @"" forHTTPHeaderField:tKey];
            }
            
        }else{
            for (NSString *tKey in self.request.allHTTPHeaderFields.allKeys) {
                [self.request setValue: @"" forHTTPHeaderField:tKey];
            }
//            for (NSString *tKey in tDict.allKeys) {
//                [self.request setValue:tDict[tKey] ?: @"" forHTTPHeaderField:tKey];
//            }
            //自定义请求头内容
            for (NSString *tKey in header.allKeys) {
                [self.request setValue:header[tKey] ?: @"" forHTTPHeaderField:tKey];
            }
            
            for (NSString *tKey in self.httpHeaders.allKeys) {
                [self.request setValue:self.httpHeaders[tKey] ?: @"" forHTTPHeaderField:tKey];
            }
        }
    }
}

#pragma mark - 设置超时时间
- (void)setTimeout:(NSTimeInterval)timeout {;
    _timeout = timeout;
    self.manager.requestSerializer.timeoutInterval = timeout;
    self.request.timeoutInterval = timeout;
}


#pragma mark - 取消当前任务
- (void)cancelCurrentRequest
{
    if (self.currentTask) {
        [self.currentTask cancel];
    }
}

#pragma mark - 取消所有任务
- (void)cancelAllRequest
{
    [self.manager.operationQueue cancelAllOperations];
}

#pragma mark - 公共参数配置
- (NSDictionary *)configCommonParam:(NSDictionary *)param
{
    if (!self.commonParamCallBack) return param;
    
    if (!param)  param = @{};
    NSDictionary *tCommonParam = self.commonParamCallBack();
    NSMutableDictionary *tParams = param.mutableCopy;
    for (NSString *tKey in tCommonParam.allKeys) {
        [tParams setValue:tCommonParam[tKey] forKey:tKey];
    }
    return tParams.copy;
}

#pragma mark - 处理公共成功回调
- (void)handleSuccess:(id)responseObject result:(ResultCallBack)result
{
    [self clearConfig];
    if (result) {
        result(responseObject);
    }
}

#pragma mark - 处理公共错误
- (void)handleError:(NSError * _Nonnull)error result:(ResultCallBack)result
{
    [self clearConfig];
    if (result) {
        NSDictionary *tErrorDict;
        switch (error.code) {
            case -1001:
                tErrorDict = [self.helper error:DGGNetworkErrorMsgTimeout];
                break;
            case -999:
                tErrorDict = [self.helper error:DGGNetworkErrorMsgCancel];
                break;
            default:
                tErrorDict = [self.helper error:DGGNetworkErrorMsgFailed];
                break;
        }
        result(tErrorDict);
    }
}

#pragma mark - 清除一些配置
- (void)clearConfig
{
    self.content_type = @"";
}

#pragma mark - 处理设置特殊Content-Type
- (void)handleContent_type:(DGGNetworkParamType)paramType
{
    NSString *tContentType = kContentType;
    if (self.content_type && self.content_type.length) {
        tContentType = self.content_type;
    }
    
    if (paramType == DGGNetworkParams) {
        [self.manager.requestSerializer setValue:tContentType forHTTPHeaderField:kContentTypeKey];
    } else {
        [self.request setValue:tContentType forHTTPHeaderField:kContentTypeKey];
    }
}

#pragma mark - 网络异常
- (BOOL)handleNoNetwork:(ResultCallBack)result
{
    if (!self.helper.isNetworkAvailable) {
        if (result) {
            result([self.helper error:(DGGNetworkErrorMsgFailed)]);
        }
        return NO;
    }
    return YES;
}

#pragma mark - 基础配置
- (void)commonConfig
{
    self.messageKey = @"message";
    self.successCodes = @[@"200"];
}

#pragma mark - 日志打印
- (void)logUrl:(NSString *)url
         param:(NSDictionary *)param
     paramType:(DGGNetworkParamType)paramType
responseObject:(id)responseObject
{
    if (self.currentParamType != paramType) return;
    
    NSDictionary *tHeaders = paramType == DGGNetworkParams
    ? self.manager.requestSerializer.HTTPRequestHeaders
    : self.request.allHTTPHeaderFields;
    id res =  [responseObject isKindOfClass:NSError.class] ? responseObject : [self debugDescription:responseObject];
    NSLog(@"%@", [NSString stringWithFormat: @"\n===========请求URL===========\n%@\n===========请求参数===========\n%@\n===========请求头===========\n%@\n===========接口返回数据===========\n%@\n", url, [self debugDescription:param], tHeaders, res]);
}

#pragma mark - 打印格式化
- (NSString *)debugDescription:(id)object{
    
    if (object == nil) return @"";
    
    NSError *error = nil;
    //字典转成json
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted  error:&error];
    //如果报错了就按原先的格式输出
    if (error) {
        return [object debugDescription];
    }
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

#pragma mark - lazy
- (DGGNetworkHelper *)helper
{
    if (!_helper) {
        _helper = [DGGNetworkHelper shareInstance];
        _helper.messageKey = self.messageKey;
    }
    return _helper;
}

- (AFHTTPSessionManager *)manager
{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
        _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        [_manager.requestSerializer setValue:kContentType forHTTPHeaderField:kContentTypeKey];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                              @"text/javascript",
                                                              @"text/json",
                                                              @"text/html",
                                                              @"text/plain",
                                                              nil];
        _manager.requestSerializer.timeoutInterval = 25;
        _manager.operationQueue.maxConcurrentOperationCount = 100;
        _manager.securityPolicy.allowInvalidCertificates = YES;
        _manager.securityPolicy.validatesDomainName = NO;
        [_manager.responseSerializer setStringEncoding:NSUTF8StringEncoding];
    }
    return _manager;
}

- (NSMutableURLRequest *)request
{
    if (!_request) {
        _request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@""
                                                                 URLString:@""
                                                                parameters:nil
                                                                     error:nil];
        _request.timeoutInterval = 15;
        [_request setValue:kFileAcceptType forHTTPHeaderField:@"Content-Type"];
    }
    return _request;
}

@end
